﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using WaterRevenueMS.WebUI.Common;
using System.ComponentModel.Composition;
using WaterRevenueMS.Core.Service.Charge.Impl;
using WaterRevenueMS.WebUI.Extension.Filters;
using WaterRevenueMS.Domain.Common.Models;
using WaterRevenueMS.Domain.ViewModels.Charge.ChargeRecord;
using WaterRevenueMS.Core.Service.Charge;
using WaterRevenueMS.Core.Service.Customer;
using WaterRevenueMS.Componnent.Common;
using WaterRevenueMS.Componnent.Entities.Charge;
using WaterRevenueMS.WebUI.Extension.MvcHelper;
using WaterRevenueMS.Domain.ViewModels.Customer.Customer;
using WaterRevenueMS.Core.Service.BasicSet;

namespace WaterRevenueMS.WebUI.Controllers
{

    [Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class ChangeChargeController : AdminController
    {
        [Import]
        public IChargeRecordService ChargeRecordService { get; set; }
        [Import]
        private ICustomerService WaterCaliberService { get; set; }


        [AdminLayoutAttribute]
        public ActionResult Index()
        {
            var modell = new ChargeRecordModel();
            return View(modell);
        }
        [AdminPermission(PermissionCustomMode.Ignore)]
        public ActionResult List(DataTableParameter param)
        {
            int total = ChargeRecordService.ChargeRecords.Count(t => t.Jiao_FZT == 2);

            var expr = BuildSearchCriteria();

            var filterResult = ChargeRecordService.ChargeRecords.Where(expr).Select(t => new ChargeRecordModel
            {
                Id = t.Id,
                Bei_Z = t.Bei_Z,
                ReviseDT = t.ReviseDT,
                Yong_HYE = t.Customer != null ? t.Customer.Zhang_HYE : 0,
                Ben_QDS = t.Ben_QDS,
                //     Jiao_FLX = t.Jiao_FLX,
                Shang_QDS = t.Shang_QDS,
                Ben_QYL = t.Ben_QYL,
                He_JYL = t.He_JYL,
                Jiao_FJE = t.Jiao_FJE,
                UserName = t.Customer != null ? t.Customer.Hu_ZXM : "",
                Yong_HDZ = t.Customer != null ? t.Customer.Yong_HZZ : "",


            }).OrderBy(t => t.Id).Skip(param.iDisplayStart).Take(param.iDisplayLength).ToList();

            int sortId = param.iDisplayStart + 1;

            var result = from c in filterResult
                         select new[]
                        {
                          
                            sortId++.ToString(), 
                            c.UserName,  
                            c.Yong_HDZ,
                            c.Yong_HYE.ToString(),
                            
                        c.Jiao_FJE.ToString(),
                        c.ReviseDT.ToString(),
                        c.Bei_Z,
                            c.Id.ToString()
                        };

            return Json(new
            {
                sEcho = param.sEcho,
                iDisplayStart = param.iDisplayStart,
                iTotalRecords = total,
                iTotalDisplayRecords = total,
                aaData = result
            }, JsonRequestBehavior.AllowGet);
        }



        public ActionResult Create()
        {
            var model = new ChargeRecordModel();

            return PartialView(model);
        }

        public ActionResult Edit(int Id)
        {
            var model = new ChargeRecordModel();
            var modelL = new ChargeRecordModel();
            var entity = ChargeRecordService.ChargeRecords.FirstOrDefault(t => t.Id == Id);
            if (null != entity)
            {

                
                model = new ChargeRecordModel
                {
                    Id = entity.Id,
                    Jiao_FJE = entity.Jiao_FJE,
                    User_ID = entity.User_Id


                };
                modelL = new ChargeRecordModel
                {
                    Ben_CJF = entity.Ben_CJF,
                    Bei_Z = entity.Bei_Z,
                    Ben_QDS = entity.Ben_QDS,
                    Ben_QYL = entity.Ben_QYL,
                    User_ID = entity.User_Id,
                    Jiao_FLX = entity.Jiao_FLX,
                    Jiao_FZT = 1,
                    Shang_QDS = entity.Shang_QDS,
                    CreateDT = entity.CreateDT,
                    He_JYL = entity.He_JYL

                };
               
            }
            ChargeRecordService.CheXYE(model);//撤销余额
            model.Jiao_FZT = 4;
            ChargeRecordService.ChangeState(model);//更新状态
            //插入新记录
            OperationResult result = ChargeRecordService.Insert(modelL);
            return Json(result);

            
        }


        private Expression<Func<ChargeRecord, Boolean>> BuildSearchCriteria()
        {
            DynamicLambda<ChargeRecord> bulider = new DynamicLambda<ChargeRecord>();
            Expression<Func<ChargeRecord, Boolean>> expr = null;
            if (!string.IsNullOrEmpty(Request["UserName"]))
            {
                var data = Request["UserName"].Trim();
                Expression<Func<ChargeRecord, Boolean>> tmp = t => t.Customer.Hu_ZXM.Contains(data);
                expr = bulider.BuildQueryAnd(expr, tmp);
            }


            Expression<Func<ChargeRecord, Boolean>> tmpSolid = t => t.Jiao_FZT == 2;//已经缴费了
            expr = bulider.BuildQueryAnd(expr, tmpSolid);

            return expr;
        }


    }

}







